package dao

import (
	"24on/pkg/xregistry"

	"gitee.com/Frozentear/layout/internal/conf"

	"gitee.com/bethink1501/24on-library/db"
	"gitee.com/bethink1501/24on-library/zredis"

	"github.com/google/wire"
	"xorm.io/xorm"
	"xorm.io/xorm/names"

	"github.com/go-kratos/kratos/contrib/registry/etcd/v2"
	"github.com/go-kratos/kratos/v2/registry"
	clientv3 "go.etcd.io/etcd/client/v3"
)

var ProviderDao = wire.NewSet(NewData,
	NewRegister,
	provideRepo)

var provideRepo = wire.NewSet(NewUserRepo)

type Data struct {
	db    *xorm.Engine
	redis *zredis.Redis
}

func NewData(cfg *conf.Data) *Data {
	var d = new(Data)
	d.db = db.InitDb(cfg.Mysql)
	d.db.SetMapper(names.SnakeMapper{})
	d.redis = zredis.InitRedis(cfg.Redis)

	return d
}

func NewRegister(cfg *conf.Registry) registry.Registrar {
	switch cfg.Type {
	case "etcd":
		client, err := clientv3.New(clientv3.Config{
			Endpoints: []string{cfg.Addr},
		})
		if err != nil {
			panic(err)
		}

		rs := etcd.New(client)
		return rs
	default:
		return xregistry.NewMdnsRegistry()

	}

}
